<?php

App::uses('CrudController', 'Controller');
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
App::uses('User', 'Model');

class ShiftModelsController extends CrudController
{

    public $_model = 'User';

    public function admin($date = null)
    {
        $this->request->allowMethod('get');
        $this->loadModel('Line');
        $this->loadModel('Shift');
        $this->loadModel('Assist');
        if ($date === null) {
            $d = new DateTime();
            $date = $d->format('m/d/Y');
        } else {
            $date = str_replace('-', '/', $date);
        }
        $lines = $this->Line->getFullEnabled();
        $assists = $this->Assist->getEnabledWithNumber();
        $shifts = $this->Shift->getEnabledOrderByCode();
        $appBosch = new stdClass();
        $appBosch->lines = $lines;
        $appBosch->shifts = $shifts;
        $appBosch->assists = $assists;
        $appBosch->date = $date;
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Rol de turnos'));
        $this->set('description', __('Administración'));
    }


    public function saveShiftModel()
    {
        $this->request->allowMethod('get');
        $result = false;
        if ($this->request->is('ajax') === true) {
            $m = $this->_model;
            $this->loadModel($m);
            $models = json_decode($this->request->query['models']);
            $model = $models[0];
            $workDate = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $model->wd);
            $data = array(
                'id' => $model->id,
                'line_id' => $model->l,
                'shift_id' => $model->s,
                'assist_id' => $model->a,
            );
            $this->loadModel('ShiftModel');
            $result = $this->ShiftModel->save($data);
            if ($result !== false) {
                $userId = $model->u;
                $lineId = $result['ShiftModel']['line_id'];
                $result = $this->ShiftModel->getByUserAndLineAndWorkdate($userId, $lineId, $workDate->format('Y-m-d'));
                $result = array(
                    'id' => $model->id,
                    'u' => $model->u,
                    'l' => $model->l,
                    'a' => $model->a,
                    's' => $model->s,
                    'lc' => $result['l']['lc'],
                    'nom' => $result['u']['nom'],
                    'curp' => $result['u']['curp'],
                    'full_name' => $result['u']['full_name'],
                );
            }
        }
        $this->set(array('records' => $result, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function updateMaginTeams()
    {
        $this->loadModel('Team');
        $this->Team->magicTeams();
        $this->set(array('success' => true, '_serialize' => 'success'));
        $this->viewClass = 'Json';
    }

    public function week($date = null)
    {
        $this->request->allowMethod('get');
        $this->loadModel('Line');
        $this->loadModel('Shift');
        $this->loadModel('Assist');
        $this->loadModel('User');
        $this->loadModel('ShiftModel');
        if ($date === null) {
            $d = new DateTime();
            $date = $d->format('m/d/Y');
        } else {
            $date = str_replace('-', '/', $date);
        }
        $end = new DateTime($date);
        for ($i = 0; $i < 7; $i++) {
            $ymd = $end->format('Y-m-d');
            $this->ShiftModel->createRol($ymd);
            $end->modify('+1 day');
        }
        $appBosch = new stdClass();
        $appBosch->lines = $this->Line->getFullEnabledShortName();
        $appBosch->assists = $this->Assist->getShortNameEnabledWithNumber();
        $appBosch->shifts = $this->Shift->getShortNameEnabledOrderByCode();
        $appBosch->date = $date;
        $rol = $this->User->getUsersForShiftModelWeek(new DateTime($date));
        $us = array();
        $i = 0;
        foreach ($rol as $r) {
            $nom = $r['nom'];
            if (isset($us[$nom]) === false) {
                $i = 0;
                $us[$nom] = array(
                    'nom' => $nom
                );
            }
            $us[$nom]['l' . $i] = $r['l'];
            $us[$nom]['a' . $i] = $r['a'];
            $us[$nom]['s' . $i] = $r['s'];
            $i++;
        }
        $appBosch->data = array_values($us);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Rol de turnos'));
        $this->set('description', __('Semanal'));
    }

    public function getRecordsWeek($param = null)
    {
        $this->loadModel('User');

        $result = $this->User->getUsersForShiftModelWeek($startDate);
        $this->set(array('records' => $result, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($param = null)
    {
        $startDate = new DateTime($param);
        $ymd = $startDate->format('Y-m-d');
        $this->ShiftModel->createRol($ymd);
        return $this->User->getUsersForShiftModel($ymd);
    }

    protected function u($model)
    {
        return array(
            'user_id' => $model->u,
            'line_number' => $model->l,
            'shift_code' => $model->s,
            'assist_number' => $model->s,
            'work_date' => $model->wd,
        );
    }

    protected function c($model)
    {
        return array();
    }

}
